﻿function scrollNews() {
    var $self = $("#txtBoxCon");
    var lineHeight = $self.find(".itemt:last").height() + 20; //获取模块高
    $self.animate({ "marginTop": lineHeight + "px" }, 600, function () {
        $self.css({ marginTop: 0 }).find(".itemt:last").prependTo($self).hide().fadeIn(800);
    })
}
function initUserFunctions() {
    $('.hot .blocke').hover(
	  function () {
	      $(this).addClass("ac");
	      $('.more', $(this)).show();
	      var rel = $(this).attr("rel");
	      var uid = $(this).attr("id");
	      var param = { type: 5, uid: uid };
	      $.ajax({
	          type: "get",
	          url: "http://www.baomihua.com/interfaces/GetSiteUserFeed.ashx",
	          data: param,
	          dataType: "html",
	          timeout: 300000,
	          success: function (data) {
	              var tempID = "more_" + rel + "_" + uid;
	              $("#" + tempID).html(data);
	          }
	      });
	  },
	  function () {
	      $(this).removeClass("ac");
	      $('.more', $(this)).hide();
	  }
	  );
	    $('.hotl .blocke').hover(
		function () {
		    $(this).addClass("ac");
		},
		function () {
		    $(this).removeClass("ac");
		}
	 );
}
function initUserFeedFunctions() {
    var scrollTimer;
    $('#txtBoxCon').hover(function () { clearInterval(scrollTimer); }, function () { scrollTimer = setInterval(scrollNews, 4000); }).trigger('mouseleave');
    var $self = $("#txtBoxCon");
    var lineHeight = $self.find(".itemt:last").height() + 20; //获取模块高
    $self.animate({ "marginTop": lineHeight + "px" }, 600, function () {
        $self.css({ marginTop: 0 }).find(".itemt:last").prependTo($self).hide().fadeIn(800);
    });
}
var curUserID = 0;
var UserInfo = "";
var SpaceURL = "";
$(document).ready(function () {
    $.getJSON("http://login.interface.baomihua.com/userapi.asmx/GetCurrentLoginUserInfo?jsoncallback=?", { flag: "json" }, function (json) {
        var tempHtml = "";
        if (typeof (json.UserInfoList) != "undefined") {
            UserInfo = json.UserInfoList[0];
            SpaceURL = UserInfo.SpaceURL;
            curUserID = UserInfo.UserId;
            tempHtml += "<div class=\"loaded\">\n";
            tempHtml += "<div class=\"pho\"><a href=\"http://www.baomihua.com/manage/index.aspx\"><img src=\"" + UserInfo.HeadImgURL + "\" /></a></div>";
            tempHtml += "<ul><li> 您好，<a href=\"http://www.baomihua.com/manage/index.aspx\">" + UserInfo.UserName + "</a></li>";
            tempHtml += "<li>玉米数：<span><a href=\"http://www.baomihua.com/manage/useradincome.aspx\" class=\"reda\">" + UserInfo.YuMiCount + "</a></span></li>";
            tempHtml += "<li>花红数：<span><a href=\"http://29293.baomihua.com/member/centerbonus.aspx\" class=\"reda\">" + UserInfo.HuaHongCount + "</a></span></li>";
            tempHtml += "<li><a href=\"http://www.baomihua.com/manage/index.aspx\">进入用户中心</a><a class=\"loginout\" href=\"http://www.baomihua.com/ui/logout.aspx\">退出</a></li></ul></div>";
            $("#loginInfo").removeClass().addClass("blocke block_login");
            var param = { type: 8, uid: curUserID };
            $.ajax({
                type: "get",
                url: "http://www.baomihua.com/interfaces/GetSiteUserFeed.ashx",
                data: param,
                dataType: "html",
                timeout: 300000,
                success: function (data) {
                    $("#recomUserDiv").html(data);
                    initUserFunctions();
                }
            });
            param = { type: 9, uid: curUserID };
            $.ajax({
                type: "get",
                url: "http://www.baomihua.com/interfaces/GetSiteUserFeed.ashx",
                data: param,
                dataType: "html",
                timeout: 300000,
                success: function (data) {
                    $("#recomUserFeedsDiv").html(data);
                    initUserFeedFunctions();
                }
            });
        }
        else {
            tempHtml += ' <p class="former"><label>帐号：</label><span class="r"><span class="ipt"><input id="form_name" name="form_name" maxlength="50" type="text" class="txt" /></span></span></p>';
            tempHtml += '<p class="former"><label>密码：</label><span class="r"><span class="ipt"><input id="form_password" name="form_password" maxlength="20" type="password" class="txt" /></span></span></p>';
            tempHtml += '<p class="former action" id="ErrorAlert"></p>';
            tempHtml += '<p class="former action"><label title="为了确保您的信息安全，请不要在网吧或者公共机房勾选此项！" for="autoLogin" class="labelCheckbox"><input class="checkbox" type="checkbox" name="autoLogin" id="checkrememberme" value="true" tabindex="3" checked="checked">记住我</label><a href="http://www.baomihua.com/ui/findpassword.aspx" target="_blank">忘记密码？</a></p>';
            tempHtml += ' <p class="former action"><button class="iBtn login" id="btn_appUserLogin" tabindex="4">登 录</button><a href="http://www.baomihua.com/ui/reg.aspx" class="reg_now" target="_blank">马上注册</a></p>';
            tempHtml += '<div class="other_login"><span class="label">用</span>';
            tempHtml += '<ul><li><a title="人人网帐号登录" id="renrenLink" href="http://www.baomihua.com/ui/renrenlogin.aspx" target="_blank">人人网帐号登录</a></li>';
            tempHtml += ' <li><a title="新浪微博帐号登录" id="sinaLink" href="http://www.baomihua.com/ui/sinaLogin.aspx" target="_blank">新浪微博帐号登录</a></li>';
            tempHtml += '<li><a title="QQ帐号登录" id="qqLink" href="http://www.baomihua.com/ui/qqLogin.aspx" target="_blank">QQ帐号登录</a></li>';
            tempHtml += ' <li><a title="腾讯微博登录" id="tqqLink" href="http://www.baomihua.com/ui/TXWeiboLogin.aspx" target="_blank">腾讯微博登录</a></li>'
            tempHtml += '<li><a title="开心网登录" id="kaixin" href="http://www.baomihua.com/ui/kaixinLogin.aspx" target="_blank">开心网登录</a></li>';
            tempHtml += '<li><a title="百度登录" id="baidu" href="http://www.baomihua.com/ui/baiduLogin.aspx" target="_blank">百度登录</a></li></ul><span class="label">账号登录</span> </div>';
            $("#loginInfo").removeClass().addClass("block_login");
            var param = { type: 7, uid: curUserID };
            $.ajax({
                type: "get",
                url: "http://www.baomihua.com/interfaces/GetSiteUserFeed.ashx",
                data: param,
                dataType: "html",
                timeout: 300000,
                success: function (data) {
                    $("#recomUserDiv").html(data);
                    initUserFunctions();
                }
            });
            param = { type: 6, uid: curUserID };
            $.ajax({
                type: "get",
                url: "http://www.baomihua.com/interfaces/GetSiteUserFeed.ashx",
                data: param,
                dataType: "html",
                timeout: 300000,
                success: function (data) {
                    $("#recomUserFeedsDiv").html(data);
                    initUserFeedFunctions();
                }
            });
        }
        $("#loginInfo").html(tempHtml);
        $.each($('.bCmt .contant .center .p-scrollLoad .mess_box dl dd .pub-row'), function (i, n) {
            var tempVideoID = $(n).attr("vid");
            var tempVideoPic = $(n).attr("pic");
            var tempUserSpace = $(n).attr("space");
            var tempVideoTitle = $(n).attr("name");
            if (SpaceURL != "") {
                tempUserSpace = SpaceURL;
            }
            shareLink(tempVideoID, tempVideoPic, tempUserSpace, tempVideoTitle, i);
        });
        $(function () {
            $("#index a").append("<b class=\"lt\"></b><b class=\"rt\"></b>");
            $("#index").attr("class", "ac");
            var emails = ['@qq.com', '@gmail.com', '@163.com', '@126.com', "@sina.com", "@yahoo.com.cn"
                , "@hotmail.com", "@139.com", "@189.com", "@188.com", "@sohu.com", "@sogou.com", "@21cn.com", "@tom.com"];
            $('#form_name').autocomplete(emails, {
                width: 208,
                selectFirst: false,
                formatMatch: function (row) {
                    return row.toString();
                },
                formatItem: function (row) {
                    return row.toString();
                },
                highlight: function (value, term) { return value },
                noMatchShow: true,
                scroll: true,
                autoGo: function (input) { }
            });
            $("#form_name").keydown(function (event) {
                if (event.keyCode == 13) {
                    Login();
                }
            });
            $("#form_password").keydown(function (event) {
                if (event.keyCode == 13) {
                    Login();
                }
            });
            $('#btn_appUserLogin').click(function () {
                Login();
            });
            AutoLogin();
        });
    });
});

function addFav() {   // 加入收藏夹
    if (document.all) {
        window.external.addFavorite('http://www.baomihua.com', '爆米花网');
    }
    else if (window.sidebar) {
        window.sidebar.addPanel('http://www.baomihua.com', '爆米花网');
    }
}
function setHomePage(obj) {
    var aUrls = document.URL.split("/");
    var vDomainName = "http://" + aUrls[2] + "/";
    try {//IE
        obj.style.behavior = "url(#default#homepage)";
        obj.setHomePage(vDomainName);
    } catch (e) {//other
        if (window.netscape) {//Firefox
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', vDomainName);
        } else {
            document.body.style.behavior = 'url(#default#homepage)';
            document.body.setHomePage(document.URL);
        }
    }
    if (window.netscape) alert("设置首页成功！");
}

//login
$(function () {
    $("#index a").append("<b class=\"lt\"></b><b class=\"rt\"></b>");
    $("#index").attr("class", "ac");
    var emails = ['@qq.com', '@gmail.com', '@163.com', '@126.com', "@sina.com", "@yahoo.com.cn"
                , "@hotmail.com", "@139.com", "@189.com", "@188.com", "@sohu.com", "@sogou.com", "@21cn.com", "@tom.com"];
    $('#form_name').autocomplete(emails, {
        width: 208,
        selectFirst: false,
        formatMatch: function (row) {
            return row.toString();
        },
        formatItem: function (row) {
            return row.toString();
        },
        highlight: function (value, term) { return value },
        noMatchShow: true,
        scroll: true,
        autoGo: function (input) {  }
    });
    $("#form_name").keydown(function (event) {
        if (event.keyCode == 13) {
            Login();
        }
    });
    $("#form_password").keydown(function (event) {
        if (event.keyCode == 13) {
            Login();
        }
    });
    $('#btn_appUserLogin').click(function () {
        Login();
    });
    AutoLogin();
});
function Login() {
    $('#ErrorAlert').html("正在登陆中...");
    $('#ErrorAlert').hide();
    $('#ErrorAlert').slideDown();
    if ($('#form_name').val() == '') {
        $('#ErrorAlert').html('请输入您的登录帐号!');
        $('#form_name').focus();
        return false;
    }
    else if ($('#form_name').val().length < 3) {
        $('#ErrorAlert').html('请输入正确的登录帐号!');
        $('#form_name').focus();
        return false;
    }
    if ($('#form_password').val() == '') {
        $('#ErrorAlert').html('请输入您的登录密码!');
        $('#form_password').focus();
        return false;
    }
    else if ($('#form_password').val().length < 6) {
        $('#ErrorAlert').html('请输入正确的登录密码!');
        $('#form_password').focus();
        return false;
    }
    var isRemember = 0;
    if ($("#checkrememberme").attr("checked") == true) {
        isRemember = 1;
    }
    F_UserLogin($('#form_name').val(), $('#form_password').val(), "", "", isRemember);
    return false;
}
function AutoLogin() {
    if (null != Pui.cookie("LoginCookie") && Pui.cookie("LoginCookie") != "" && Pui.cookie("LoginCookie") != "LoginCookie=") {
        F_UserLogin("", "", "", "", 1);
    } else {
        var appID = $("#hid_hid_appID").val();
        var appUserID = $("#hid_appUserID").val();
        if (appID != "" && appUserID != "") {
            F_UserLogin("", "", appID, appUserID, 1);
        }
    }
}
function F_UserLogin(strUserName, strUserPWD, appliID, appUID, isRemember) {
    var param = { userName: strUserName, password: strUserPWD, appID: appliID, appuid: appUID, remember: isRemember };
    var postUrl = "http://login.interface.baomihua.com/userapi.asmx/UserLogin";
    $.ajax({
        type: "get",
        url: postUrl,
        data: param,
        dataType: "jsonp",
        jsonp: "jsoncallback",
        timeout: 300000,
        success: function (result) {
            if (result.Result == "1") {
                var userName = strUserName;
                var httpurl = "http://youxi.baomihua.com/logindo.aspx?form_name=" + userName + "&userInfo=" + result.UserInfo + "&loginInfo=" + result.LoginInfo;
                var _frm = document.createElement("iframe");
                _frm.style.display = "none";
                _frm.src = httpurl;
                document.body.appendChild(_frm);
                /*var httpurl2 = "http://cu.365dianying.com/logindo.aspx?form_name=" + userName + "&userInfo=" + result.UserInfo + "&loginInfo=" + result.LoginInfo;
                var _frm2 = document.createElement("iframe");
                _frm2.style.display = "none";
                _frm2.src = httpurl2;
                document.body.appendChild(_frm2);*/

                Pui.cookie('UserInfo', result.UserInfo, {
                    path: '/',
                    domain: '.baomihua.com',
                    expires: 30
                });
                Pui.cookie('LoginInfo', result.LoginInfo, {
                    path: '/',
                    domain: '.baomihua.com',
                    expires: 30
                });
                Pui.cookie('LoginCookie', result.LoginCookie, {
                    path: '/',
                    domain: '.baomihua.com',
                    expires: 30
                });
                var httpurl3 = "http://www.baomihua.com/pomoho.aspx";
                $.ajax({
                    type: "get",
                    url: httpurl3,
                    data: { McSession: result.LoginSessionID },
                    dataType: "jsonp",
                    jsonp: "jsoncallback",
                    timeout: 300000,
                    success: function (result) {
                        window.location.href = "http://www.baomihua.com/";
                    }
                });
                var tempDate = new Date();
                tempDate.setTime(tempDate.getTime() + (30 * 24 * 60 * 60 * 1000));
                document.cookie = "McSession=" + result.LoginSessionID + ";expires=" + tempDate.toUTCString();
            }
            else {
                if (result.LoginType == 2) {
                    window.location.href = "http://www.baomihua.com/manage/userbind.aspx";
                } else if (result.LoginType == 1) {
                    Pui.cookie('LoginCookie', "", {
                        path: '/',
                        domain: '.baomihua.com',
                        expires: -1
                    });
                }
                switch (result.Result) {
                    case "-1":
                        $("#ErrorAlert").html("登陆帐号不存在");
                        break;
                    case "-2":
                        $("#ErrorAlert").html("登陆帐号与密码不匹配");
                        break;
                    case "-3":
                        $("#ErrorAlert").html("登陆帐号或密码错误");
                        break;
                    case "-4":
                        $("#ErrorAlert").html("无法自动登录，您的密码有误");
                        break;
                    case "-5":
                        $("#ErrorAlert").html("无法自动登录，您的密码有误2");
                        break;
                    case "-6":
                        $("#ErrorAlert").html("你已经暂时被禁止登录，请注意你在网站的行为！");
                        break;
                    default:
                        $("#ErrorAlert").html("可能网络原因，造成登录失败");
                        break;
                }
            }
        }
    });
}
